package com.cuitrip.component.para;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.ColorRes;
import android.util.AttributeSet;
import android.util.TypedValue;

import com.cuitrip.app.MainApplication;
import com.cuitrip.component.para.model.Para;
import com.cuitrip.component.para.model.TimPara;
import com.cuitrip.component.para.ui.ParaHolderView;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.adapter.BaseHolderView;
import com.lab.adapter.HolderViewAdapter;
import com.lab.component.list.LinearListView;
import com.lab.utils.DisplayUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ak on 15/10/19.
 */
public class ParaListView<T extends Para> extends LinearListView {

    private HolderViewAdapter mAdapter;
    private List<T> paraList = new ArrayList<>();
    private int color;
    private int contentColor;
    private float circleSize;

    public ParaListView(Context context) {
        this(context, null);
    }

    public ParaListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initInternal(context, attrs);
    }

    private void initInternal(Context context, AttributeSet attrs) {
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        setPadding(DisplayUtil.dip2px(16.f), DisplayUtil.dip2px(8.f), DisplayUtil.dip2px(16.f),paddingBottom);
        if (attrs!=null){
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ParaHolderView);
            circleSize = a.getDimensionPixelSize(R.styleable.ParaHolderView_circleSize, getResources().getDimensionPixelSize(R.dimen.ct_ts_11));
            a.recycle();
        } else {
            circleSize = getResources().getDimensionPixelSize(R.dimen.ct_ts_11);
        }
        color = ResourceUtil.getColor(R.color.ganshi_ded8d7);
        contentColor=ResourceUtil.getColor(R.color.qiaomai_7c706e);
        mAdapter = new HolderViewAdapter(MainApplication.getInstance(), paraList, ParaHolderView.class);
        mAdapter.setHolderViewCallback(new HolderViewAdapter.HolderViewCallback() {
            @Override
            public void onHolderViewInvalidate(BaseHolderView holderView, int position) {
                if (holderView instanceof ParaHolderView) {
                    ParaHolderView paraHolderView = (ParaHolderView) holderView;
                    paraHolderView.setCircleIconColor(color);
                    paraHolderView.setTextColor(contentColor);
                    paraHolderView.setCircleIconSize(TypedValue.COMPLEX_UNIT_PX, circleSize);
                    if (paraList.get(position) instanceof TimPara && paraList.size() > 1) {
                        paraHolderView.setShowTopLine(true);
                    }
                }
            }
        });
        setAdapter(mAdapter);
    }

    public void setParas(List<T> list) {
        paraList.clear();
        paraList.addAll(list);
        mAdapter.notifyDataSetChanged();
        if (paraList.isEmpty()) {
            setVisibility(GONE);
        } else {
            setVisibility(VISIBLE);
        }
    }

    public void setCircleColor(@ColorRes int colorResId) {
        color = ResourceUtil.getColor(colorResId);
    }

    public void setTextColor(@ColorRes int colorResId) {
        contentColor = ResourceUtil.getColor(colorResId);
    }

}
